﻿using System;
using System.Collections.Generic;
using System.Text;
using Silvermoon.OpenGL;
using System.Drawing;

namespace Silvermoon.Shapes
{
    public class HighlightStringShape : StringShape
    {

        protected override TextSprite CreateTextSprite()
        {
            return new HighlightTextSprite();
        }

        protected HighlightTextSprite HighlightSprite { get { return TextSprite as HighlightTextSprite; } }

        public Color HighlightTextColor
        {
            get { return HighlightSprite.HighlightColor; }
            set
            {
                var s = HighlightSprite;
                if (s.HighlightColor != value)
                {
                    s.HighlightColor = value;
                    Invalidate();
                }
            }
        }

        public void Highlight(int index)
        {
            HighlightSprite.Highlight(index);
            Invalidate();
        }

        public void Highlight(params int[] indexes)
        {
            HighlightSprite.Highlight(indexes);
            Invalidate();
        }

        public void HighlightRange(int from, int to)
        {
            HighlightSprite.HighlightRange(from, to);
            Invalidate();
        }

        public void ClearHighlights()
        {
            HighlightSprite.ClearHighlights();
            Invalidate();
        }

        /// <summary>
        /// Highlights all appearances of a given text.
        /// </summary>
        /// <param name="subString">text to highlight.</param>
        public void Highlight(string subString)
        {
            ClearHighlights();
            if (!string.IsNullOrEmpty(subString))
            {
                int startIndex = 0;
                int len = subString.Length;
                int index = Text.IndexOf(subString, startIndex, StringComparison.OrdinalIgnoreCase);
                while (index >= 0)
                {
                    for (int i = index; i < index + len; i++) Highlight(i);
                    startIndex += len;
                    index = Text.IndexOf(subString, startIndex, StringComparison.OrdinalIgnoreCase);
                }
            }
            Invalidate();
        }
    }
}
